
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.Coupon;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:获取当前游戏可用代金券信息
 *
 * @author wcj
 * @since 2016年5月9日 上午10:41:47
 */
public class RequestCouponInfoTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 请求可用代金券信息
     * 
     * @param type 1：游戏 2：游币
     * @return
     */
    public CouponInfoResponse request(int type) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        CouponInfoResponse respObj = new CouponInfoResponse();
        ResponsePackage resp = new PatchInfoResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GET_COUPON_BY_GAMEID);
        params.put("type", type);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_GET_COUPON_BY_GAMEID);

        User user = UserManager.getInstance().getCurrentUserInternal();
        String userName = user.getAccountName();
        req.setUserName(userName);
        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class PatchInfoResponsePackage extends HttpBaseResponsePackage<CouponInfoResponse> {

        @Override
        public void handleResponse(CouponInfoResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "RequestCouponInfoTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (response.isUseful() && cmd.equals(ProtocolCmd.CMD_GET_COUPON_BY_GAMEID)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        JSONArray couponDatas = data.optJSONArray("couponlist");
                        response.setShowYoubiRechargeCoupon(data.optInt("couponstatus") == 1);
                        if (couponDatas != null && couponDatas.length() > 0) {
                            ArrayList<Coupon> coupons = new ArrayList<Coupon>();
                            for (int j = 0; j < couponDatas.length(); j++) {
                                Coupon coupon = new Coupon();
                                coupon.setId(couponDatas.optJSONObject(j).optInt("couponid"));
                                coupon.setMoney(couponDatas.optJSONObject(j).optInt("money"));
                                coupon.setMoneyLimit(couponDatas.optJSONObject(j).optInt(
                                        "needmoneylimit"));
                                coupon.setCouponName(couponDatas.optJSONObject(j).optString(
                                        "couponname"));
                                coupon.setEndtime(couponDatas.optJSONObject(j).optLong("endtime") * 1000);
                                coupon.setLockTime(couponDatas.optJSONObject(j).optLong("locktime") * 1000);
                                coupon.setLockStatus(couponDatas.optJSONObject(j).optInt(
                                        "lockstatus"));

                                coupons.add(coupon);
                            }
                            response.setCouponInfos(coupons);
                        }

                        response.setOk(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应消息
     */
    public static class CouponInfoResponse extends ResponseMessage {
        ArrayList<Coupon> couponInfos;

        // 显示游币兑换界面代金券
        boolean showYoubiRechargeCoupon;

        public ArrayList<Coupon> getCouponInfos() {
            return couponInfos;
        }

        public void setCouponInfos(ArrayList<Coupon> couponInfos) {
            this.couponInfos = couponInfos;
        }

        public boolean isShowYoubiRechargeCoupon() {
            return showYoubiRechargeCoupon;
        }

        public void setShowYoubiRechargeCoupon(boolean showYoubiRechargeCoupon) {
            this.showYoubiRechargeCoupon = showYoubiRechargeCoupon;
        }

    }
}
